Matching = function (string) {
	"use strict";
	var seed = {};
	seed.id = getUniqueId();
	var sidebar = $('body').find('.side-bar .middle'),
		preview = $('body').find('.preview'),
		doc = $(document);
		
	seed.name = "";
	seed.qtext = "";
	seed.atexts1 = [];
	seed.atexts2 = [];
	seed.feedback = "";
	seed.type = "matching";
	
	seed.genXML = function () {
		var self = this;
		var xmlstring = "";
		
		//Matching Question Header
		//xmlstring += "\t<question type=\"category\">\n\t\t<category><text>$course$/" + self.category + "</text>\n\t</category>\n</question>";
		xmlstring += "<question type=\"matching\">\n\t<name>\n\t\t<text><![CDATA[<p>" + self.name + "</p>]]></text>\n\t</name>";
		xmlstring += "<questiontext format=\"html\"><text><![CDATA[<p>" + self.qtext + "</p>]]></text></questiontext>\n";
		xmlstring += writeXMLAnswers(self);
		xmlstring += "\n\t<generalfeedback><text><![CDATA[<p>" + self.feedback + "</p>]]></text></generalfeedback>\n\t<shuffleanswers>true</shuffleanswers>\n</question>";
		return xmlstring;
	};
	
	var writeXMLAnswers = function(self){
		var temp = "";
		
		for(var i = 0; i < self.atexts1.length; i++) {
			temp += "\n\t<subquestion>\n\t\t<text><![CDATA[<p>" + self.atexts1[i] + "</p>]]></text>\n\t\t<answer>\n\t\t\t";
			temp += "<text><![CDATA[<p>" + self.atexts2[i] + "</p>]]></text>\n\t\t</answer>\n\t</subquestion>";
		}
		return temp;
	}
	
	var writeGIFTAnswers = function(self){
		var temp = "";
			
		for(var i = 0; i < self.atexts1.length; i++) {
			temp += "=" + self.atexts1[i] + " -> " + self.atexts2[i];
		}
		return temp;
	}
	
	seed.genGift = function () {
		var self = this;
		var giftString = "";
		//write categories
		giftString = "$CATEGORY: " + self.category;
		//write question
		giftString += "::" + self.name + "::" + self.qtext + "{";
		giftString += writeGIFTAnswers(self);
		giftString += "}\n\n";
		return giftString;
	};
	
	seed.genQSD = function () {
		var self = this;
		var qsdString = "";
		
		qsdString += "MATCHBANK:\r\n";
		qsdString += "" + self.name + "; " + self.qtext + "; " + self.feedback + "\r\n";
		for(var i = 0; i < self.atexts1.length; i++)
		{
			qsdString += "" + self.atexts1[i] + "; " + self.atexts2[i] + "; \r";
		}
		qsdString += "\r\n";
		return qsdString;
	};
	
	seed.validate = function () {
		var self = this,
			error = "";
		if(self.name === "") {
			error += "Missing field(s)";
		} else if(self.qtext === "") {
			error += "Missing field(s)";
		}
		
		for (var index = self.atexts1.length - 1; index >= 0; index--) {
			if(self.atexts1[index] === "" || self.atexts2[index] === "") {
				if(self.atexts1[index] === "" && self.atexts2[index] === "") {
					self.atexts1.splice(index,1);
					self.atexts2.splice(index,1);
				} else {
					if(error !== "") {
						error += ", ";
					}
					error += "Missing an item in pair " + (index + 1);
				}
			}
		}
		
		if(self.atexts1.length === 0) {
			if(error !== "") {
				error += ", ";
			}
			error += "There must be at least one matched pair";
		}
		return error;
	};
	
	seed.set = function (options) {
		var self = this;
		$.each(options, function (key, value) {
			if (key === 'name') {
				self.name = value;
			} else if (key === 'qtext') {
				self.qtext = value;
			} else if (key === 'feedback') {
				self.feedback = value;
			} else if (key === 'atexts1') {
				self.atexts1 = value;
			}else if (key === 'atexts2'){
				self.atexts2 = value;
			}
		});
	};
		
	if (string) {
		// spilt the string up by line
		var self = seed,
			lines = string.split("\r\n"),
			currentLine = 2;
			
		if (lines.length === 1) {
			lines = string.split("\n");
		}
		
		// set up the options object
		var obj = {};
		obj.name = "";
		obj.qtext = "";
		obj.atexts1 = [];
		obj.atexts2 = [];
		obj.feedback = "";
		obj.category = "";
		
		lines[1] = lines[1].replace("\\;", ";\\");
		var data = lines[1].split(/;(?!\\)/);
		$.each(data, function(index) {
			data[index] = data[index].replace(";\\", "\\;");
		});
		obj.name = data[0].trim();
		obj.qtext = data[1].trim();
		obj.feedback = data[2].trim();
		
		
		// go through the lines with array data
		// trim is to get rid of leading and trailing whitespace
		lines[2] = lines[2].replace("\\;", ";\\");
		var pairs = lines[2].split(/;(?!\\)/);
		for (var i = 0; i < pairs.length - 1; i += 2) {
			obj.atexts1.push(pairs[i].trim());
			obj.atexts2.push(pairs[i + 1].trim());
		}
		
		seed.set(obj);
	
	};
	
	return seed;
};
